package studio.wetrack.dinner.db.access;

/**
 * Created by zhanghong on 2017/7/29.
 */
public class UserDBAccessor {

//    String DRIVER = "com.mysql.jdbc.Driver";
//    String URL = "jdbc:mysql://test.waids.cn:3306/dinner";
//    String USERNAME = "wetrack";
//    String PASSWORD = "wetrack";
//
//    String sql_select_all = "select * from user";
//    String sql_insert = "insert into user (`id`, `name`, `id_num`, `birthday`, `sex`) values (?, ?, ?, ?, ?)";
//
//    Connection connection;
//    public UserDBAccessor() throws SQLException, ClassNotFoundException {
//        Class.forName(DRIVER);
//        connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//    }
//
//    public void shutDown() throws SQLException {
//        if(connection != null){
//            if(!connection.isClosed()){
//                connection.close();
//            }
//        }
//    }
//
//    public List<User> selectAll() throws SQLException {
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql_select_all);
//        while(resultSet.next()){
//            User user = new User();
//            user.setId(resultSet.getLong("id"));
//            user.setName(resultSet.getString("name"));
//            user.setIdNum(resultSet.getString("id_num"));
//            user.setBirthday(resultSet.getDate("birthday"));
//            user.setSex(User.Sex.valueOf(resultSet.getString("sex")));
//            System.out.println(user.toString());
//        }
//
//        return null;
//    }
//
//    public boolean insert(User user) throws SQLException {
//        PreparedStatement pStatement = connection.prepareStatement(sql_insert);
//        pStatement.setLong(1, user.getId());
//        pStatement.setString(2, user.getName());
//        pStatement.setString(3, user.getIdNum());
//        pStatement.setDate(4, new java.sql.Date(user.getBirthday().getTime()));
//        pStatement.setString(5, user.getSex().toString());
//        return pStatement.execute();
//    }
//
//    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        UserDBAccessor dbAccessor = new UserDBAccessor();
//        User user = new User();
//        user.setId(4);
//        user.setName("zxd");
//        user.setIdNum("330326199101010101");
//        user.setBirthday(new java.util.Date());
//        user.setSex(User.Sex.MALE);
//        dbAccessor.insert(user);
//        dbAccessor.selectAll();
//        dbAccessor.shutDown();
//    }
}
